// pages/mine/edit/index.js
const app = getApp();
var serverUrl = app.globalData.serverUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eyeclose: 1,
    eyeUrl: "/asset/img/eye-hide.png",
    eye_open: "/asset/img/eye-show.png",
    eye_close: "/asset/img/eye-hide.png",
    eye_type: "password",
    sex:[
      { name: 0, value: "男", checked: false },
      { name: 1, value: "女", checked: false }
    ],
    sexValue:0,
    multiIndex: [0, 0, 0],
    multiArray:[],
    objectMultiArray:{},
    customItem: '全部',
    userInfo:{},
    picker:0,
    selectMessage:"请选择",
    selectValue:[],
    prov:0,
    city:0,
    area:0,
    disbtn:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userInfo = wx.getStorageSync("userInfo");
    that.setData({
      userInfo:userInfo
    })
    let sex = that.data.sex;
    for (let key in sex) {
      if(userInfo.sex == sex[key].name){
        sex[key].checked=true;
      }
    }
    that.setData({
      sex:sex,
      sexValue:userInfo.sex
    })
    wx.request({
      url: serverUrl+'api/v1/common/map/getpca/pid/0',
      method:"GET",
      success:function(data){
        data = data.data;
        if(data.code == 200){
          let arr = [];
          for(let key in data.data){
            arr.push(data.data[key].name);
          }
          let res = []; let result= [];
          result = that.data.multiArray;
          result[0] = arr;
          res = that.data.objectMultiArray;
          res[0] = data.data;
          that.setData({
            multiArray: result,
            objectMultiArray:res
          })
          /**获取城市**/
          var ucity = 1;
          if (userInfo.prov) ucity = userInfo.prov;
          wx.request({
            url: serverUrl + 'api/v1/common/map/getpca/pid/' + ucity,
            method: "GET",
            success: function (data) {
              data = data.data;
              if (data.code == 200) {
                let arr = [];
                for (let key in data.data) {
                  arr.push(data.data[key].name);
                }
                let res = []; let result = [];
                result = that.data.multiArray;
                result[1] = arr;
                res = that.data.objectMultiArray;
                res[1] = data.data;
                that.setData({
                  multiArray: result,
                  objectMultiArray: res
                })
                /**获取地区**/
                var uarea = 37;
                if (userInfo.city) uarea = userInfo.city;
                wx.request({
                  url: serverUrl + 'api/v1/common/map/getpca/pid/' + uarea,
                  method: "GET",
                  success: function (data) {
                    data = data.data;
                    if (data.code == 200) {
                      let arr = [];
                      for (let key in data.data) {
                        arr.push(data.data[key].name);
                      }
                      let res = []; let result = [];
                      result = that.data.multiArray;
                      result[2] = arr;
                      res = that.data.objectMultiArray;
                      res[2] = data.data;
                      that.setData({
                        multiArray: result,
                        objectMultiArray: res
                      })
                      let provk = 0;
                      let cityk = 0;
                      let areak = 0;
                      let uareas = 0;
                      if (userInfo.area) uareas = userInfo.area;
                      let str = '';
                      for (let keys in res) {
                        for (let ke in res[keys]) {
                          if (keys == 0 && res[keys][ke].id == ucity) {
                            provk = ke;
                            str += res[keys][ke].name;
                          }
                          if (keys == 1 && res[keys][ke].id == uarea) {
                            cityk = ke;
                            str += ' ' + res[keys][ke].name;
                          }
                          if (keys == 2 && res[keys][ke].id == uareas) {
                            areak = ke;
                            str += ' ' + res[keys][ke].name;
                          }
                        }
                      }
                      if (str) {
                        that.setData({
                          selectMessage: str,
                        })
                      }
                      that.setData({
                        area: uareas,
                        prov: ucity,
                        city: uarea,
                        multiIndex: [provk, cityk, areak]
                      })
                    }
                  }
                })
                /**获取地区**/
              }
            }
          })
          /**获取城市**/
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 密码显示点击事件 
   */
  eye: function (e) {
    var that = this;
    let id = e.currentTarget.id;
    if (id == 1) {
      that.setData({
        eyeclose: 0,
        eyeUrl: that.data.eye_open,
        eye_type: "text"
      })
    } else {
      that.setData({
        eyeclose: 1,
        eyeUrl: that.data.eye_close,
        eye_type: "password"
      })
    }
  },

  /**
   * 选择器更改事件
   */
  bindMultiPickerChange:function(e){
    console.log(e);
    var that = this;
    let value = e.detail.value;
    let string = '';
    string += that.data.multiArray[0][value[0]] + ' ' + that.data.multiArray[1][value[1]] + ' ' + that.data.multiArray[2][value[2]];
    let prov = that.data.objectMultiArray[0][value[0]].id;
    let city = that.data.objectMultiArray[1][value[1]].id;
    let area = that.data.objectMultiArray[2][value[2]].id;
    that.setData({
      prov: prov,
      city: city,
      area: area,
      selectMessage: string,
      selectValue: value
    })
  },

  /**
   * 选择器列更改事件 
   */
  bindMultiPickerColumnChange:function(e){
    var that = this;
    let column = e.detail.column;
    let value = e.detail.value;
    if(column == 1) {
      that.setData({
        picker: value
      })
    }
    if(column < 2){
      let id = 0;
      let data = {};
      data = that.data.objectMultiArray[column][value];
      wx.request({
        url: serverUrl + 'api/v1/common/map/getpca/pid/'+data.id,
        method:"GET",
        success:function(data){
          data = data.data;
          if(data.code == 200){
            let arr = [];
            for (let key in data.data) {
              arr.push(data.data[key].name);
            }
            let res = []; let result = [];
            result = that.data.multiArray;
            result[column + 1] = arr;
            res = that.data.objectMultiArray;
            res[column + 1] = data.data;
            that.setData({
              multiArray: result,
              objectMultiArray: res
            })
            if (column == 0) {
              let ids = that.data.objectMultiArray[1][that.data.picker].id;
              wx.request({
                url: serverUrl + 'api/v1/common/map/getpca/pid/' + ids,
                method: "GET",
                success: function (data) {
                  data = data.data;
                  if (data.code == 200) {
                    let arr = [];
                    for (let key in data.data) {
                      arr.push(data.data[key].name);
                    }
                    let res = []; let result = [];
                    result = that.data.multiArray;
                    result[2] = arr;
                    res = that.data.objectMultiArray;
                    res[2] = data.data;
                    that.setData({
                      multiArray: result,
                      objectMultiArray: res
                    })
                  }
                }
              })
            }
          }
        }
      })
    }
  },

  /**资料提交**/
  editPost:function(res){
    var that = this;
    that.setData({
      disbtn:true
    })
    let nickname = res.detail.value.nickname;
    if(!nickname || nickname.length > 10 || nickname.length < 1) {
      wx.showToast({
        title: '昵称不可为空且不得大于十个字符不得少于一个字符',
        icon:"none",
        duration:1500
      })
      that.setData({
        disbtn: false
      })
      return false;
    }
    let password = res.detail.value.password;
    let pv = /^[0-9a-zA-Z_]{6,18}$/;
    if(password && !pv.test(password)){
      that.setData({
        disbtn: false
      })
      wx.showToast({
        title: '密码格式错误【密码格式:数字字母下划线6-18位】',
        icon:'none',
        duration:1500
      })
      return false;
    }
    let confirm = res.detail.value.confirm;
    if(confirm != password){
      that.setData({
        disbtn: false
      })
      wx.showToast({
        title: '确认密码与密码不相等',
        icon:"none",
        duration:1500
      })
      return false;
    }
    var data = {};
    if(password){
      data.password = password;
    }
    data.nickname = nickname;
    data.prov = that.data.prov;
    data.city = that.data.city;
    data.area = that.data.area;
    data.sex = that.data.sexValue;
    if(!data.prov || !data.city || !data.area){
      that.setData({
        disbtn: false
      })
      wx.showToast({
        title: '请选择省市区',
        icon: "none",
        duration: 1500
      })
      return false;
    }
    data.uid = wx.getStorageSync("userInfo").uid;
    wx.request({
      url: serverUrl+'api/v1/users/addinfos',
      method:"POST",
      dataType:"json",
      data:data,
      success:function(data){
        that.setData({
          disbtn: false
        })
        data = data.data;
        console.log(data);
        if(data.code == 200){
          let userInfo = {};
          userInfo.nickname = data.data.nickname;
          userInfo.area = data.data.area;
          userInfo.city = data.data.city;
          userInfo.prov = data.data.prov;
          userInfo.email = data.data.email;
          userInfo.headimg = data.data.headimg;
          userInfo.uid = data.data.id;
          userInfo.lat = data.data.lat;
          userInfo.lon = data.data.lon;
          userInfo.member_type = data.data.member_type;
          userInfo.mobile = data.data.mobile;
          userInfo.number = data.data.number;
          userInfo.sex = data.data.sex;
          wx.setStorageSync("userInfo", userInfo);
          wx.showToast({
            title: '编辑成功',
            icon: "success",
            duration: 1500
          })
        } else {
          wx.showToast({
            title: '编辑失败!【'+data.message+'】',
            icon:"none",
            duration:1500
          })
        }
      },
      error:function(data){
        that.setData({
          disbtn: false
        })
        wx.showToast({
          title: '服务器错误!编辑失败',
          icon:"none",
          duration:1500
        })
      }
    })
  },

  /**radio更改事件**/
  radioChange:function(res){
    let value = res.detail.value;
    this.setData({
      sexValue:value
    })
  }
})